/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswt;

import net.miginfocom.swt.MigLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreWidget;
import spagswt.SpaglContainer;

import org.eclipse.swt.SWT;

/**
 * A Composite (org.eclipse.swt.widgets.Composite) that conforms to the SpaglCoreContainer interface
 * @author Sam Pottinger
 */
public class SpaglContainer implements SpaglCoreContainer, SpaglSwtContainer, SpaglSwtWidget {
	
	private Composite innerWidget;
	private SpaglCoreContainer parent;
	private MigLayout layoutManager;
	
	/**
	 * Creates a bare bones container
	 * 
	 * @param newParent the {@link SpaglContainer} that will be the parent of this widget
	 */
	public SpaglContainer(SpaglCoreContainer newParent)
	{
		innerWidget = new Composite(((SpaglSwtContainer)newParent).getComposite(), SWT.NORMAL);
		innerWidget.setLayout(new MigLayout("fillx", "[left]rel[grow,fill]", "[]10[]"));
		parent = newParent;
	}
	
	/**
	 * Creates a container with specified parameters for the layout manager
	 * @param newParent the {@link SpaglContainer} to be the parent of this widget
	 * @param placementParam The parameters to passed to the layout manager
	 */
	public SpaglContainer(SpaglCoreContainer newParent, String placementParam)
	{
		innerWidget = new Composite(((SpaglSwtContainer)newParent).getComposite(), SWT.NORMAL);
		innerWidget.setLayout(new MigLayout("fillx", "[left]rel[grow,fill]", "[]10[]"));
		parent = newParent;
		parent.placeWidget(this, placementParam);
	}
	
	/**
	 * Creates a container with the specified layout manager parameters and layout manager
	 * @param newParent The parent of this widget
	 * @param placementParam The parameters passed to the layout manager
	 * @param newLayoutManager A (at this moment in development) MigLayout object
	 */
	public SpaglContainer(SpaglCoreContainer newParent, String placementParam, MigLayout newLayoutManager)
	{
		innerWidget = new Composite(((SpaglSwtContainer)newParent).getComposite(), SWT.NORMAL);
		layoutManager = newLayoutManager;
		innerWidget.setLayout(layoutManager);
		parent = newParent;
		parent.placeWidget(this, placementParam);
	}

	/**
	 * Gets the native SWT Composite object for native placement (no cross-library capabilites)
	 */
	public Composite getComposite() {
		return innerWidget;
	}

	/**
	 * Places a widget on this container with the given string parameters
	 * @param targetWidget the target widget to be placed in this container
	 * @param parameters the string parameters for the layout manager
	 */
	public void placeWidget(SpaglCoreWidget targetWidget, String parameters) {
		((SpaglSwtWidget)targetWidget).getNativeWidget().setLayoutData(parameters);
	}
	
	/**
	 * Places a native SWT widget on this container with the given string parameters
	 * @param targetWidget the target widget to be placed in this container
	 * @param parameters the string parameters for the layout manager
	 */
	public void placeWidget(Control targetWidget, String parameters) {
		targetWidget.setLayoutData(parameters);
	}

	/**
	 * Gets the native Composite widget (SWT only)
	 * 
	 * @return the SWT Composite widget that this wraps
	 */
	public Control getNativeWidget() {
		return innerWidget;
	}

	/**
	 * Gets the parent container for this widget
	 * 
	 * @return Returns the {@link SpaglContainer} that is the holding this widget
	 */
	public SpaglCoreContainer getContainer() {
		return parent;
	}

	/**
	 * Gets the height of this widget
	 * 
	 * @return the vertical size of the widget
	 */
	public int getHeight() {
		return innerWidget.getBounds().y;
	}

	/**
	 * Gets the width of this widget
	 * 
	 * @return the horizontal size of the widget
	 */
	public int getWidth() {
		return innerWidget.getBounds().x;
	}

	/**
	 * Sets the size of this widget
	 * 
	 * @param width the horizontal size of this object
	 * @param height the vertical size of this object
	 */
	public void setSize(int width, int height) {
		innerWidget.setSize(width, height);
	}
	
	/**
	 * Calls the native dipose method on this widget
	 */
	public void dispose()
	{
		innerWidget.dispose();
	}

}
